package cn.edu.scau.cmi.chensixiao.javaFX;

import cn.edu.scau.cmi.chensixiao.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.chensixiao.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.chensixiao.composite.consistent.ConsistentLeaf;
import cn.edu.scau.cmi.chensixiao.composite.safe.SafeComponent;
import cn.edu.scau.cmi.chensixiao.composite.safe.SafeComposite;
import cn.edu.scau.cmi.chensixiao.composite.safe.SafeLeaf;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;

public class CompositeController {

	@FXML
	private Label nameLabel;
	@FXML
	private Label idLabel;
	@FXML
	private ImageView photo;
	@FXML
	private Button safe;
	@FXML
	private Button consistent;
	@FXML
	private Label choiceLabel;
	@FXML
	private ChoiceBox<String> nameChoice;
	@FXML
	private TextArea messageText;
	
	
	private ConsistentComposite c1;
	private ConsistentLeaf p1;
	private ConsistentLeaf pp1;
	private ConsistentComposite c2;
	private ConsistentLeaf p2;
	private ConsistentLeaf pp2;
	private ConsistentComposite c3;
	private ConsistentLeaf p3;
	private ConsistentLeaf pp3;
	
	private SafeComposite s1;
	private SafeLeaf sp1;
	private SafeComposite s2;
	private SafeLeaf sp2;
	private SafeComposite s3;
	private SafeLeaf sp3;


	public void initialize() {
		nameChoice.getItems().addAll("人事部","财务部","公关部","张三","李四","王五","张小三","李小四","王小五");	
		
	}
	
	public void createConsistentComponent() {
		c1 = new ConsistentComposite("人事部");
		 p1 = new ConsistentLeaf("张三");
		 pp1 = new ConsistentLeaf("张小三");
		c1.addChild(p1);
		c1.addChild(pp1);
		
		
		 c2 = new ConsistentComposite("财务部");
		 p2 = new ConsistentLeaf("李四");
		 pp2 = new ConsistentLeaf("李小四");
		c2.addChild(p2);
		c2.addChild(pp2);
		
		 c3 = new ConsistentComposite("公关部");
		 p3 = new ConsistentLeaf("王五");
		 pp3 = new ConsistentLeaf("王小五");
		c3.addChild(p3);
		c3.addChild(pp3);
	}
	
	public void createSafeComponent() {
		 s1 = new SafeComposite("人事部");
		 sp1 = new SafeLeaf("张三");
		s1.addChild(sp1);
		
		 s2 = new SafeComposite("财务部");
		 sp2 = new SafeLeaf("李四");
		s2.addChild(sp2);
		
		 s3 = new SafeComposite("公关部");
		 sp3 = new SafeLeaf("王五");
		s3.addChild(sp3);

	}
	
	public void buttonOnClick1() {
		createConsistentComponent();
		ConsistentComponent cc = null;
		ConsistentLeaf cp = null;
		String nameString = nameChoice.getValue();
		switch(nameString) {
		case"人事部":cc = c1;break;
		case"张三":cp=p1;break;
		case"张小三":cp=pp1;break;
		case"财务部":cc=c2;break;
		case"李四":cp=p2;break;
		case"李小四":cp=pp2;break;
		case"公关部":cc=c3;break;
		case"王五":cp=p3;break;
		case"王小五":cp=pp3;break;
		}
		messageText.setText("");
		if(cc!=null) {
			   messageText.appendText(cc.showMessage()+'\n');
			   messageText.appendText(nameString+"部门下有成员：  ");
			for(ConsistentComponent component : cc.getChildren()) {
				messageText.appendText(component.name+"、  ");
			}
		}
		else {
			messageText.appendText(cp.showMessage()+"不可再分");
		}
	}
	
	public void buttonOnClick2() {
		createSafeComponent();
		SafeComposite sc = null;
		SafeLeaf sp = null;
		String nameString = nameChoice.getValue();
		switch(nameString) {
		case"人事部":sc = s1;break;
		case"张三":sp = sp1;break;
		case"财务部":sc = s2;break;
		case"李四":sp = sp2;break;
		case"公关部":sc = s3;break;
		case"王五":sp = sp3;break;
		//default:
		}
		messageText.setText(""); 
		if(sc != null) {
			   messageText.appendText(sc.showMessage()+'\n');
			   messageText.appendText(nameString+"部门下有成员：  ");
			for(SafeComponent component : sc.getChildren() ) {
				messageText.appendText(component.name + "、  ");
			}
		}
		
		else if(sp != null) {
			messageText.appendText(sp.showMessage()+"不可再分，没有叶子节点");
		}
		else {
			messageText.appendText("安全模式下查无此人");
		}
	}
	
}



